#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QToolButton>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->setWindowTitle("CameraTool-调试");

    ui->scrollArea->setFrameShape(QFrame::Box);  //Panel  NoFrame
    //QLabel{border:10px dotted #242424;}


    QStringList list;

    list << tr("Detect Point")
         << tr("Detect Circle")
         << tr("Intersection")
         << tr("Measure Dista")
         <<tr("Measure Width")
         <<tr("Measure Line")
         <<tr("Measure Angle");


    QWidget * pWgt = new QWidget;
    //QHBoxLayout *pLayout = new QHBoxLayout();   //网格布局
    //QVBoxLayout*
    pLayout = new QVBoxLayout();

    QToolButton *pBtn = new QToolButton();

    //pBtn->setIcon(QIcon("F:\\1.png"));      //为工具按钮设置图标，图标来自于 F 盘的 1.png 文件
    //pBtn->setText(QString("算法%1").arg(0));

    pBtn->setText(QString("%1 开始 ").arg(0));

    pBtn->setMinimumSize(QSize(120,30));   //width height

    pBtn->setStyleSheet("QToolButton{text-align : left;}");

    pLayout->addWidget(pBtn);           //把按钮添加到布局控件中


    //QString s = QString("a = %1, b = %2, c = %3").arg(200).arg(list[1]).arg(11.5);
    //qDebug()<<s;


    for(int i = 1; i < 10; i++)
    {
        //QToolButton *pBtn = new QToolButton();
        pBtn = new QToolButton();


        //pBtn->setStyleSheet("QToolButton{text-align : left;}");
        // 设Qtoolbutton样式  https://blog.csdn.net/weixin_41937297/article/details/120753797
        pBtn->setStyleSheet("QToolButton{border: none; background: rgb(68, 69, 73); color: rgb(0, 160, 230);text-align : left;}");

       // pBtn->set (Qt.AlignRight);

       // QString str = list[i];

        QString s = QString("%1 %2").arg(i).arg(list[i%6]);
        qDebug()<<s;
        pBtn->setText(s);
        //pBtn->setText(QString("%1 算法").arg(i) );
        pBtn->setMinimumSize(QSize(120,30));   //width height
        pLayout->addWidget(pBtn);           //把按钮添加到布局控件中
    }
    pLayout->addStretch(); //在最下面 当于有一个弹簧顶着
    pWgt->setLayout(pLayout);

    //这一句setWidget必须放在pWgt里面的内容都准备完毕之后，否则显示有问题
    ui->scrollArea->setWidget(pWgt);
     ui->scrollArea->setWidgetResizable(true);
    //setCentralWidget(scrollArea);
}

MainWindow::~MainWindow()
{
    delete ui;
}

//QT动态添加删除控件
//https://blog.csdn.net/m0_73443478/article/details/130410502
void MainWindow::on_pushButton_4_clicked()
{
    //pLayout->;
    //pLayout->Stretch();
    QToolButton *pBtn = new QToolButton();

    pBtn->setText(QString("%1 算法").arg(100) );
    pBtn->setMinimumSize(QSize(120,30));   //width height

    //pLayout->addWidget(pBtn);           //把按钮添加到布局控件中

    QList<QToolButton *> labelList= ui->scrollArea->findChildren<QToolButton *>();
    qDebug()<< "before insertWidget "<< labelList.length() ;
    pLayout->insertWidget(labelList.length(),pBtn);

    QList<QToolButton *> labelList2= ui->scrollArea->findChildren<QToolButton *>();
    qDebug()<< "after insertWidget "<< labelList2.length() ;


    //pLayout->insertWidget();
    //pLayout->addStretch();
}

 //  https://blog.csdn.net/m0_73443478/article/details/130410502
void MainWindow::on_pushButton_5_clicked()
{
    //删除所有Label 子控件
    static int iiii=3;
    QList<QToolButton *> labelList= ui->scrollArea->findChildren<QToolButton *>();

    //for(int i=0;i<labelList.length();++i)

    qDebug()<< "Item length "<< labelList.length() ;

    if(iiii < labelList.length())
    {
        QToolButton *fm =labelList.at(iiii++);
        pLayout->removeWidget(fm);
        fm->deleteLater();
    }

}

